﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NKD07.Admin
{
    public partial class statistic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadMonthsYears();
            }
        }

        private void LoadMonthsYears()
        {
            // load all months for statistics
            List<int> months = LoadMonths();
            // load all years for statistics
            List<int> years = LoadYears();

            if (months.Count != 0 && years.Count != 0)
            {
                ddlMonth.Items.Clear();
                ddlYear.Items.Clear();

                // insert months into dropdownlist "ddlMonth"
                ListItem item1 = new ListItem();
                item1.Value = "-1";
                item1.Text = "-- chọn tháng --";
                ddlMonth.Items.Add(item1);

                foreach (int month in months)
                {
                    item1 = new ListItem();
                    item1.Value = month.ToString();
                    item1.Text = month.ToString();
                    ddlMonth.Items.Add(item1);
                }

                ListItem item2 = new ListItem();
                item2.Value = "-1";
                item2.Text = "-- chọn năm --";
                ddlYear.Items.Add(item2);
                // insert years into dropdownlist "ddlYear"
                foreach (int year in years)
                {
                    item2 = new ListItem();
                    item2.Value = year.ToString();
                    item2.Text = year.ToString();
                    ddlYear.Items.Add(item2);
                }

                ddlMonth.SelectedIndex = 0;
                ddlYear.SelectedIndex = 0;
            }
        }

        private List<int> LoadMonths()
        {
            Classes.warehouse warehouse = new Classes.warehouse();
            List<int> months = warehouse.GetAllMonths();
            return months;
        }

        private List<int> LoadYears()
        {
            Classes.warehouse warehouse = new Classes.warehouse();
            List<int> years = warehouse.GetAllYears();
            return years;
        }

        // show all statistics for products in warehouse 
        protected void btnChoise_Click(object sender, EventArgs e)
        {
            try
            {

                int month = int.Parse(ddlMonth.SelectedValue);
                int year = int.Parse(ddlYear.SelectedValue);
                Classes.warehouse warehouse = new Classes.warehouse();
                DataTable tb = warehouse.getProductsInWarehouse(month, year);
                gwProductStatistics.DataSource = tb;
                gwProductStatistics.DataBind();
            }
            catch (Exception)
            {
                Response.Redirect("default.aspx");
            }
        }
    }
}